


strLengthValidator();//验证输入长度
(function(){
	var msgloadDiv = null;
	$(function () {
		msgloadDiv = $("#msgLoadDiv").loading({"zIndex":2000});
		initViewWindow();//初始化短信发送窗口	
	});
	
	
	/**
	 * 初始化短信发送窗口
	 * @returns {Function}
	 */
	function initViewWindow(){
		//设置弹窗
		var viewWindow = $("#messageDialog") ;		
		viewWindow.dialog({
	        autoOpen: false,
	        width: 450,
	        height: 350,
	        resizable: false,
	        title: "发送短信",
	        modal: true,
	        close: function(){
	        	$(this).empty();
	        }
	    });
		viewWindow.dialog("open");		
		//$("#messageDiv").height(getWindowHeight()-450);
		$("#messageDiv").height(300);
		//声明消息提示
	    var toolTips = new Object();
	
	    //设置表单验证
	    var validator = $("#sendMessageForm").validate({
	        rules:{
	        	messageContent:{
	        		required:true,
	        		strMaxLenth:"900",
	        	}
	        },
	        messages:{
	            
	        },
	        errorPlacement: function(eror,element){
	            var toolTip = createTooltip(toolTips,element.attr("id"));
	            toolTip.setContent(eror.text());
	            toolTip.close();
	            element.addClass("error-border");
	        },
	        success:function(label){
	            var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
	            var toolTip = toolTips[elementId];
	            $("#" + elementId).removeClass("error-border");
	            toolTip.close();
	            toolTip.detach();
	        },
	        submitHandler:function(form){
	        	msgloadDiv.showLoading();
	        	post(getPath() + "base/userPhone/sendMessage",
	        		$("#sendMessageForm").serialize(),
	                function(resultObject){
	                var errors = resultObject.errors;
	                var results =eval("("+ resultObject.result  +")"); 
	                //判断后台是否有返回错误消息
	                if(errors && errors.length > 0){
	                	msgloadDiv.hideLoading();
	                    for(var i=0; i < errors.length; i++){
	                        var error = errors[i];
	                        var toolTip = createTooltip(toolTips,error.field);
	                        toolTip.setContent(error.defaultMessage);
	                        $("#" + error.field).addClass("error-border");
	                        consle.log(error.defaultMessage);
	                    }
	                }else{
	                    //添加成功，弹出添加成功提示框
	                	msgloadDiv.hideLoading();
	                	viewWindow.dialog("close");
	        			$('#messageDialog').empty();
	        			createWindow(results.status).dialog("open");
	                }
	        	},function(resultObject){
	        		msgloadDiv.hideLoading();
	        		createWindow(resultObject).dialog('open');
	        	});
	        }
	    });
		//取消
		$('#cancleBtn').click(function(){
			viewWindow.dialog("close");
			$('#messageDialog').empty();
		});
	}
	
	
})();